package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ListMapper;
import com.goods.business.service.ListService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ListQueryVo;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.apache.poi.ss.formula.functions.PPMT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Service
public class ListServiceImpl implements ListService {
    @Autowired
    private ListMapper listMapper;

    // 展示物资资料
    @Override
    public PageVO<Product> findProductList(ListQueryVo listQueryVo) {
        // 分类id
        String name = listQueryVo.getName();
        Integer status = listQueryVo.getStatus();
        String categorys = listQueryVo.getCategorys();

        Product product = new Product();
        String[] split = null;
        if(!StringUtils.isEmpty(categorys)){
            split = categorys.split(",");
            product.setOneCategoryId(Long.valueOf(split[0]));
            product.setTwoCategoryId(Long.valueOf(split[1]));
            product.setThreeCategoryId(Long.valueOf(split[2]));
        }


        if(!StringUtils.isEmpty(name)){
            product.setName(name);
        }
        if(status!=null){
            product.setStatus(status);
        }
        List<Product> list = listMapper.findProductList(product);

        Integer pageNum = listQueryVo.getPageNum();
        Integer pageSize = listQueryVo.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<Product> productPageInfo = new PageInfo<>(list);
        PageVO<Product> productPageVO = new PageVO<>(productPageInfo.getTotal(), list);

        return productPageVO;
    }

    // 添加物资资料 添加默认状态是审核
    @Override
    public void add(Product product) {
        listMapper.insert(product);
    }

    // 编辑物资资料-根据id查询
    @Override
    public Product editList(Long id) {
        Product product = new Product();
        product.setId(id);
        return listMapper.selectByPrimaryKey(product);
    }

    // 编辑物资资料
    @Override
    public void updateList(Long id, Product product) {
        product.setId(id);
        listMapper.updateByPrimaryKey(product);
    }

    // 更改物资状态 status:0物资正常,1:物资回收,2:物资审核中
    @Override
    public void updateStatus(Long id, int status) {
        Product product = new Product();
        product.setId(id);
        product = listMapper.selectOne(product);
        product.setStatus(status);
        listMapper.updateByPrimaryKey(product);
    }

    // 删除物资
    @Override
    public void deleteList(Long id) {
        Product product = new Product();
        product.setId(id);
        listMapper.delete(product);
    }
}
